package com.shujiaotong.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shujiaotong.common.R;
import com.shujiaotong.pojo.Card;
import com.shujiaotong.service.ICardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-20
 */
@RestController
@RequestMapping("/card")
@Slf4j
public class CardController {

    @Autowired
    private ICardService cardService;

//    分页
    @GetMapping("page")
    public R page(
            @RequestParam int page,
            @RequestParam int pageSize,
            @RequestParam(required = false) String name){
    //        接收参数
        log.info("当前页:{},页条数:{},姓名:{}",page,pageSize,name);
    //        分页条件
        Page<Card> cardPage = new Page<>(page,pageSize);
    //        查询条件
        LambdaQueryWrapper<Card> qw = new LambdaQueryWrapper<>();
        qw.like(!StringUtils.isEmpty(name),Card::getName,name);
    //        分页查询
        qw.orderByDesc(Card::getUpdateTime);
        qw.orderByAsc(Card::getName);
        cardService.page(cardPage,qw);
    //        返回值
        return R.success(cardPage);
    }

//新增
    @PostMapping
    public R save(@RequestBody Card card) {
//        log.info("新增会员卡:{}",card);
        boolean save = cardService.save(card);
        return save ? R.success("新增成功") : R.error("新增失败");
    }

//    修改
    @PutMapping
    public R update(@RequestBody Card card) {
//        log.info("修改会员卡:{}",card);
        boolean update = cardService.updateById(card);
        return update ? R.success("修改成功") : R.error("修改失败");
    }

//    根据id查询
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id ){
        log.info("根据id查询会员卡:{}",id);
        Card card = cardService.getById(id);
        return R.success(card);
    }


//    删除
    @DeleteMapping
    public R delete(Long id) {
        log.info("根据id删除会员卡:{}",id);
        boolean removeById = cardService.removeById(id);
        return removeById ? R.success("删除成功") : R.error("删除失败");
    }


//    查询拥有此卡的用户

    //前端传入cardid，通过usercard表查询对应的所有的userid，再通过userid查询user

    @GetMapping("user/{id}")
    public R getUserByCardId(@PathVariable Long id) {
        log.info("根据cardId查询用户:{}",id);
        Object userCardsByUserid = cardService.getUserCardsByUserid(id);
        return R.success(userCardsByUserid);
    }

//    获取会员卡列表
    @GetMapping("getCardList")
    public R getCardList(
            @RequestParam(required = false) String type
    ){
        LambdaQueryWrapper<Card> qw = new LambdaQueryWrapper<>();
        qw.eq(!StringUtils.isEmpty(type),Card::getType,type);
        qw.orderByDesc(Card::getUpdateTime);
        List<Card> list = cardService.list(qw);
        return R.success(list);
    }
}
